/*******************************************************************************
 * Copyright (c) 2012 Till Kolditz.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 * created on 10.10.2012 at 12:44:36
 * 
 *  Contributors:
 *      Till Kolditz
 *******************************************************************************/
package de.cc.yFilesParser.wizard;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.ide.IDE;

import de.cc.yFilesParser.Activator;
import de.kolditz.common.ui.dialog.ErrorDialog;

/**
 * @author Till Kolditz - Till.Kolditz@GoogleMail.com
 */
public class ConvertWizard extends Wizard
{
    ConvertStartPage startPage;
    ConvertSettingsPage settingsPage;

    /**
     * 
     */
    public ConvertWizard()
    {
        setHelpAvailable(false);
        setNeedsProgressMonitor(true);
        setDialogSettings(Activator.getDefault().getDialogSettings());
        setWindowTitle("Convert yFile Wizard");
    }

    @Override
    public void addPages()
    {
        addPage(startPage = new ConvertStartPage(this));
        addPage(settingsPage = new ConvertSettingsPage(this));
    }

    @Override
    public boolean performFinish()
    {
        try
        {
            if (startPage.isSetStandard())
            {
                startPage.saveAsStandard();
            }
            if (settingsPage.isSetStandard())
            {
                settingsPage.saveAsStandard();
            }
            getContainer().run(true, true, new ConvertGraphmlToSMLRunnable(this));

            IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), startPage
                    .getTargetFile().toURI(), EditorsUI.DEFAULT_TEXT_EDITOR_ID, true);
            return true;
        }
        catch (InvocationTargetException e)
        {
            e.getCause().printStackTrace();
            new ErrorDialog(getShell(), e.getCause()).open();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            new ErrorDialog(getContainer().getShell(), e).open();
        }
        return false;
    }
}
